@extends("layout.admin")

@section("title")
    用户列表
@endsection

@section("content")
    {{--修改样式--}}
    <link href="{{asset('css/table.css')}}" rel="stylesheet"/>

    <div class="layui-card-body ">
        {{--搜索--}}
        <form class="layui-form layui-col-space5 layui-input-inline" id="search-form">
            <div class="demoTable">
                <div class="layui-inline">
                    <input class="layui-input" name="name" id="name" style="width: 100% !important;"
                           value="{{request('name')}}" placeholder="请输入账号名称">
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="real_name" id="real_name" style="width: 100% !important;"
                           value="{{request('real_name')}}" placeholder="请输入真实姓名">
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="mobile" id="mobile" style="width: 100% !important;"
                           value="{{request('mobile')}}" placeholder="请输入电话号码">
                </div>
                <div class="layui-input-inline locked_ch">
                    <select id="choose" name="is_locked">
                        <option @if(request("is_locked")=="") selected @endif value="">--用户状态--</option>
                        <option @if(request("is_locked")=="1") selected @endif value="1">禁用</option>
                        <option @if(request("is_locked")=="0") selected @endif value="0">启用</option>
                    </select>
                </div>
                @if(session('guard')=="admin")
                    <div class="layui-input-inline" style="width: 185px;">
                        <div id="org"></div>
                    </div>
                @endif
                <div class="layui-inline layui-show-xs-block">
                    <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
        </form>
    </div>
    {{--列表--}}
    <div class="layui-card-body">
        <script type="text/html" id="toolbar">
            {{--添加--}}
            @can("admin.user.create")
            <div class="layui-inline layui-show-xs-block">
                <a onclick="admin.openLayerForm('{{ route("admin.user.create") }}', '添加用户', 'POST', '500px', '520px',0,'#create_from')"
                   class="layui-btn layui-btn-sm" data-type="auto" id="create"><i class="layui-icon"></i>添加</a>
            </div>
            @elsecan("system.user.create")
            <div class="layui-inline layui-show-xs-block">
                <a onclick="admin.openLayerForm('{{ route("system.user.create") }}', '添加用户', 'POST', '500px', '520px',0,'#create_from')"
                   class="layui-btn layui-btn-sm" data-type="auto" id="create"><i class="layui-icon"></i>添加</a>
            </div>
            @endcan
        </script>
        <table class="layui-table" lay-filter="table-button" lay-data="{toolbar: '#toolbar',defaultToolbar:['filter'], limit:  {{$error==0?$user['per_page']:12}} }">
            <thead>
            <tr>
                <th lay-data="{field:'id',width:80, fixed: 'left', unresize: true, sort: true}">编号</th>
                <th lay-data="{field:'name'}">名称</th>
                <th lay-data="{field:'real_name'}">姓名</th>
                <th lay-data="{field:'email'}">邮箱</th>
                <th lay-data="{field:'mobile'}">电话</th>
                <th lay-data="{field:'org_id'}">机构</th>
                <th lay-data="{field:'site_id',hide:true}">站点</th>
                @can("admin.user.onoff")
                    <th lay-data="{field:'lock_deadline'}">账号状态</th>
                @endcan
                @can("system.user.onoff")
                    <th lay-data="{field:'lock_deadline'}">账号状态</th>
                @endcan
                <th lay-data="{field:'login_time'}">上次登录时间</th>
                <th lay-data="{field:'login_ip'}">上次登录ip</th>
                <th lay-data="{field:'created_at',hide:true}">创建时间</th>
                <th lay-data="{field:'updated_at',hide:true}">更新时间</th>
                <th lay-data="{field:'op',fixed: 'right', width:100, align:'center',toolbar: '#buttons'}">操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($user["data"] as $u)
                <tr>
                    <td>{{ $u['id'] }}
                    </td>
                    <td>{{ $u['name'] }}</td>
                    <td>{{ $u['real_name'] }}</td>
                    <td>{{ $u['email'] }}</td>
                    <td>{{ $u['mobile'] }}</td>
                    <td>{{ isset($u['org']['name'])?$u['org']['name'] : "尚未分配" }}</td>
                    <td>{{ isset($u["site"]["name"] )?$u['site']['name'] : "全站" }}</td>
                    @can("admin.user.onoff")
                        <td>
                            <a class="layui-btn-xs">
                                <input type="checkbox" @if($u['is_locked']==0) checked="" @endif name="open"
                                       data-id="{{$u['id']}}" lay-skin="switch"
                                       lay-filter="switchLocked" lay-text="ON|OFF">
                            </a>
                        </td>
                    @endcan
                    @can("system.user.onoff")
                        <td>
                            <a class="layui-btn-xs">
                                <input type="checkbox" @if($u['is_locked']==0) checked="" @endif name="open"
                                       data-id="{{$u['id']}}" lay-skin="switch"
                                       lay-filter="switchLocked" lay-text="ON|OFF">
                            </a>
                        </td>
                    @endcan
                    <td>{{ $u['login_time']>0?date("Y-m-d H:i:s",$u['login_time']):"尚未登录"}}</td>
                    <td>{{ $u['login_ip']}}</td>
                    <td>{{ $u['created_at']}}</td>
                    <td>{{ $u['updated_at']}}</td>
                    <td data-field="controller">
                        <script type="text/html" id="buttons">
                            <a class="layui-btn layui-btn-xs" lay-event="more">管理 <i
                                    class="layui-icon layui-icon-down"></i></a>
                        </script>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        @include("layout.page")
    </div>
@endsection

@section('script')
    <script src="{{asset('layui/modules/xm-select.js')}}"></script>
    <script src="{{asset('js/tree/select.js')}}"></script>
    <script>
        var idx = -1;
        const error = {{$error}};
        layui.use(['form','dropdown', 'table', 'layer', 'xmSelect'], function () {
            var table = layui.table,
                dropdown = layui.dropdown,
                form = layui.form,
                layer = layui.layer,
                xmSelect = layui.xmSelect;

            //操作按钮组事件
            table.on('tool(table-button)', function (obj) {
                var that = this
                    , res = obj.data;
                //更多下拉菜单
                let buttons = [
                        @if(_can(session("guard").".user.show")){title: '查看详情', id: 'show',type:'link',link:"{{ route_uri(session("guard").".user.show")}}",params:{user: res.id}},@endif
                        @if(_can(session("guard").".user.change_pwd")){title: '修改密码', id: 'edit_pwd_from',type:"openLayerForm",link:"{{route_uri(session("guard").".user.change_pwd")}}",params:{user_id: res.id},set:{title:"修改密码",method:"POST",width:"500px",height:"308px",repush:0,dome:"#edit_pwd_from"}},@endif
                        @if(_can(session("guard").".user.edit")){title: '编辑资料', id: 'edit',type:"link",link:"{{route_uri(session("guard").".user.edit")}}",params:{user: res.id}},@endif
                        @if(_can(session("guard").".user.role_form")){title: '分配权限', id: 'role_form',type:"link",link:"{{route_uri(session("guard").".user.role_form")}}",params:{user: res.id}}@endif]

                dropdown.render(tableButtonGroup(that,buttons))
            });

            //监听用户状态开关
            form.on('switch(switchLocked)', function (data) {
                let status = this.checked ? 0 : 1;
                const id = data.elem.getAttribute('data-id');
                $.ajax({
                    url: '{{session('guard')=='system'?route('system.user.onoff'):route('admin.user.onoff')}}',
                    data: {"id": Number(id), "is_locked": status},
                    method: "get",
                    success: function (res) {
                        if (res.error > 0) {
                            //错误框
                            layer.msg(res.msg, {
                                anim: 6,
                                icon: 5,
                                time: 3000
                            });
                            if (res.error == 70001) {
                                location.reload();
                            }
                            return false;
                        }
                        layer.msg(res.msg, {icon: 6});
                    }
                });
            });

            let params = selectTreeParams('#org', 'org_id', '请选择', "{{request('org_id')}}", JSON.parse('{!! $orgs !!}'), res => function () {
            });
            xmSelect.render(params)

            //分页显示
            @if($error==0)
            admin.paginate("{{ $user['total'] }}", "{{ $user['current_page'] }}", "{{ $user['per_page'] }}");
            @endif
        });
    </script>

    <script src="{{asset('js/layCommon.js')}}"></script>
@endsection
